package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivitySosContactBinding;
import com.apex.aolonfitdemo.dialog.WaitingDialog;

import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;

import com.cyber.bridge.module.bean.SosCallEntity;
import com.cyber.bridge.module.listener.OnCyberSOSContactListener;

import java.util.ArrayList;
import java.util.List;


public class SosContactActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    int maxCount;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(SosContactActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivitySosContactBinding activitySosContactBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activitySosContactBinding = ActivitySosContactBinding.inflate(getLayoutInflater());
        setContentView(activitySosContactBinding.getRoot());

        activitySosContactBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activitySosContactBinding.tool);
        getSupportActionBar().setTitle(null);
        activitySosContactBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnSOSContactListener(new OnCyberSOSContactListener() {
            @Override
            public void setSOSContactSwitch(boolean b) {

            }

            @Override
            public void sendSOSContacts(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x41 : 0x43);
                }

            }

            @Override
            public void querySOSContactMaxCount(int i) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                maxCount = i;

            }
        });

        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(SosContactActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().querySOSContactMaxCount();
        activitySosContactBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = activitySosContactBinding.num.getText().toString();
                String name = activitySosContactBinding.name.getText().toString();
                if (TextUtils.isEmpty(num)) {
                    return;
                }

                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(SosContactActivity.this);
                }
                waitingDialog.show();
                List<SosCallEntity> entityList = new ArrayList<>();
                entityList.add(new SosCallEntity(name, num));
                AolonFitManager.getInstance().sendSOSContacts(entityList);
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }


        super.onDestroy();
    }
}
